package com.jd.service;

import com.jd.mapper.UserMapper;
import com.jd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(User user) {
        //获取密码
        String password = user.getPassword();
        //将密码转为byte数组保存
        byte[] bytes = password.getBytes();
        //将密码加密
        String md5 = DigestUtils.md5DigestAsHex(bytes);
        System.out.println(md5);
        //将密码交给user
        user.setPassword(md5);
        //根据用户名和密码查库
        User userDB = userMapper.findUserByUP(user);
        //如果密码或用户名不正确 返回空
        if (userDB==null){
            return null;
        }
        String token = UUID.randomUUID().toString();
        return token;
    }

    @Override
    public void insert(User user) {
        //设置username
        user.setUsername(user.getUsername());
        //获取用户输入的密码
        String password = user.getPassword();
        //将密码进行加密并入库
        byte[] bytes = password.getBytes();
        String md5 = DigestUtils.md5DigestAsHex(bytes);
        user.setPassword(md5);
        user.setPhone(user.getPhone());
        userMapper.insert(user);
    }
}
